﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shop.Core.Enums;
using Shop.Core.Share;
using Shop.IServices;
using Shop.Model.Dto.Orders;
using Shop.Model.View.Order;

namespace Shop.WebApi.Controllers
{
    /// <summary>
    /// 订单
    /// </summary>
    [ApiController]
    [Route("Api/[controller]")]
    public class OrderController : ShopControllerBase
    {
        private readonly IOrdersAppService _ordersAppService;

        private readonly IPaymentAppService _paymentAppServicecs;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordersAppService"></param>
        /// <param name="paymentAppServicecs"></param>
        public OrderController(IOrdersAppService ordersAppService
            , IPaymentAppService paymentAppServicecs)
        {
            _ordersAppService = ordersAppService;
            _paymentAppServicecs = paymentAppServicecs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("CreateOrder")]
        public async Task<AppResult<long>> CreateOrder(OrderAddDto input)
        {
            return await _ordersAppService.Create(input);
        }

        /// <summary>
        /// 升级订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("UpgradeOrder")]
        public async Task<AppResult<long>> CreateGiftBagOrder(GiftBagOrderAddDto input)
        {
            return await _ordersAppService.CreateGiftBagOrder(input);
        }
        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("Pay")]
        public async Task<AppResult<object>> Pay(PayDto input)
        {
            if (input.PayMode == Payment.Balance && string.IsNullOrWhiteSpace(input.PayPwd))
                return Problem("请输入交易密码");
            return await _paymentAppServicecs.Pay(input);
        }
        

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("List")]
        public AppResult<PageModel<OrderListVo>> GetOrderList([FromQuery]PageSearchModel<int> input)
            => _ordersAppService.GetOrderList(input);

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("Detail")]
        public async Task<AppResult<OrderDetailVo>> GetOrderDetail([FromQuery] SimpleDto<long> input)
        {
            try
            {
                return await _ordersAppService.GetOrderDetail(input.Value);
            }
            catch (Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("AddComment")]
        public async Task<AppResult> AddComment(OrderEvaluateDto input)
        {
            if (input.items == null || input.items.Count == 0)
                return Problem("评论不能为空");
            foreach (var item in input.items)
            {
                if(item.Images.Length > 5)
                    return Problem("评论照片最多五张");
            }

            try
            {
                await _ordersAppService.AddComment(input);
                return Success();
            }catch (Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 收货
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("OrderConfirm")]
        public async Task<AppResult> OrderConfirm(SimpleDto<long> input)
        {
            try
            {
                await _ordersAppService.OrderConfirm(input.Value);
                return Success();
            }
            catch (Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("CancelOrder")]
        public async Task<AppResult> CancelOrder(SimpleDto<long> input)
        {
            try
            {
                await _ordersAppService.CancelOrder(input.Value);
                return Success();
            }
            catch(Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 获取订单数（未支付、待发货、待收货、待评价）
        /// </summary>
        /// <returns></returns>
        [HttpGet("OrderNum")]
        public async Task<AppResult<Dictionary<int, int>>> GetOrderNum()
            => await _ordersAppService.GetOrderNum();
        
        /// <summary>
        /// 完成订单支付
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("AdminPay")]
        [AllowAnonymous]
        public AppResult AdminPay(AdminPayDto dto)
            => _paymentAppServicecs.AdminPay(dto);

        /// <summary>
        /// 物流追踪
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("LogisticsTracking")]
        public async Task<AppResult<List<LogisticsTrackingVo>>> LogisticsTracking([FromQuery] SimpleDto<long> input)
            => await _ordersAppService.LogisticsTracking(input.Value);
    }
}
